//
//  TestPlayerController.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/11/13.
//  Copyright © 2018 IOS3. All rights reserved.
// //    playerModel.videoURL = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];


#import "TestPlayerController.h"
#import <WMPlayer/WMPlayer.h>
#import "FullScreenPlayerController.h"


@interface TestPlayerController () <WMPlayerDelegate>

@property (nonatomic, strong) WMPlayer *player;
@property (nonatomic, assign) BOOL havNav;

@end

@implementation TestPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    
    self.hbd_barHidden = false;
    
    self.havNav = true;
    
    WMPlayerModel *localPlayerModel = WMPlayerModel.new;
    localPlayerModel.title = @"本地视频播放";
    NSURL *url = [NSURL fileURLWithPath:[NSBundle.mainBundle pathForResource:@"testMV" ofType:@"mp4"]];
    localPlayerModel.videoURL = [NSURL URLWithString:url.absoluteString];
    WMPlayer *localPlayer = [WMPlayer playerWithModel:localPlayerModel];
    localPlayer.delegate = self;
//    localPlayer.isFullscreen = true;
    self.player = localPlayer;
    [self.view addSubview:localPlayer];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    UIDeviceOrientation orientation = UIDevice.currentDevice.orientation;
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 0;
    CGFloat h = 0;
    
    x = 0;
    y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
    w = CGRectGetWidth(self.view.frame);
    h = w*9/16;
    
    self.player.frame = CGRectMake(x, y, w, h);
//    if (orientation == UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeLeft == UIDeviceOrientationLandscapeRight) {
//
//    }
//    else {
//
//        //
//        x = 0;
//        y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
//        w = CGRectGetWidth(self.view.frame);
//        h = w*9/16;
//
//        self.player.frame = CGRectMake(x, y, w, h);
//
//    }
DLog(@"%@", NSStringFromCGRect(CGRectMake(x, y, w, h)));
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (self.player.superview==nil) {
        [self.view addSubview:self.player];
    }
    else if (self.player.superview != self.view) {
        [self.player removeFromSuperview];
        [self.view addSubview:self.player];
    }
    
}

- (void)wmplayer:(WMPlayer *)wmplayer clickedFullScreenButton:(UIButton *)fullScreenBtn {
    
    FullScreenPlayerController *fullScreenPlayer = FullScreenPlayerController.new;
    fullScreenPlayer.player = self.player;
    [self.player removeFromSuperview];
    
//    __weak typeof(self)weakSelf = self;
//    fullScreenPlayer.fullScreenPlayerDismissed = ^(WMPlayer * _Nonnull player) {
//        [weakSelf.view addSubview:player];
//        [weakSelf viewWillLayoutSubviews];
//    };
    
    [self presentViewController:fullScreenPlayer animated:NO completion:nil];
}


//- (BOOL)shouldAutorotate {
//    return YES;
//}
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//
//    UIDeviceOrientation orientation = UIDevice.currentDevice.orientation;
//    [self.player removeFromSuperview];
//    switch (orientation) {
//        case UIDeviceOrientationLandscapeLeft:
//            [UIApplication.sharedApplication.keyWindow addSubview:self.player];
//            self.havNav = NO;
//            self.player.isFullscreen = true;
//
//            return UIInterfaceOrientationMaskLandscapeRight;
//            break;
//        case UIDeviceOrientationLandscapeRight:
//            [UIApplication.sharedApplication.keyWindow addSubview:self.player];
//            self.havNav = NO;
//            self.player.isFullscreen = true;
//
//            return UIInterfaceOrientationMaskLandscapeLeft;
//            break;
//        default:
//            [self.view addSubview:self.player];
//            self.havNav = YES;
//            self.player.isFullscreen = false;
//            return UIInterfaceOrientationMaskPortrait;
//            break;
//    }
//}



@end
